<%@ include file="/WEB-INF/views/taglib.jsp"%>
<h3>Asset Model Form</h3>

<form:form id="form" method="post" modelAttribute="assetTransaction" cssClass="simpleForm">
	<form:hidden path="assetType" />
	<div class="clear" style="height:1px">&nbsp;</div>
	<jsp:include page="/WEB-INF/views/message.jsp"></jsp:include>
	<fieldset><legend>Equipment Initial Balance</legend>
	<c:choose>
	<c:when test="${assetTransaction.assetTransactionId==null}">
	<div> <br> </div>
					<input type="button" id="file-uploader" value="Import" class="button positiveDark right"/>
					<input type="button" class="button positiveDark right" value = "Template" onclick="window.open('<c:url value="/resources/sample/initialbalance.xls"></c:url>')" />
	<div> <br> </div>	 
	
	</c:when>
	</c:choose>
	<table id="initialBalanceTable" class="gridSearch">
		<thead>
			<tr>
				<th>Company Code</th>
				<th>Location</th>
				<th>Serial No</th>
				<c:if test="${assetTransaction.assetTransactionId==null}">
				<th>Remarks</th>
				</c:if>
			</tr>
		</thead>
		<tbody>
			<c:choose>
				<c:when test="${not empty assetTransaction.assetTransactionItems}">
					<c:forEach items="${assetTransaction.assetTransactionItems}" var="item" varStatus="status">
						<tr>
							<td>${item.companyCode}</td>
							<td>${item.location.locationName}</td>
							<td>${item.serialNo}</td>
							<c:if test="${assetTransaction.assetTransactionId==null}">
							<td>${item.remarks}</td>
							</c:if>
						</tr>
					</c:forEach>
				</c:when>
				<c:otherwise>
					
				</c:otherwise>
			</c:choose>

		</tbody>
	</table>
	 
	<div class="action"><c:choose>
		<c:when test="${assetTransaction.assetTransactionId==null}">
			<button type="button" class="button positive" onclick="formSubmit()">
	  			Save
	  			</button>
			<a href="<c:url value="/assetModel/update/${assetModel.assetModelId}" />" class="button">Back</a>
		</c:when>
		<c:when test="${assetTransaction.assetTransactionId!=null}">
			<c:url value="/assetModel/initialBalance/${assetModel.assetModelId}" var="initialBalanceUrl"></c:url>
			<a href="<c:url value="/assetModel/update/${assetModel.assetModelId}" />" class="button">Back</a>

		</c:when>
	</c:choose></div>
</form:form>
<table id="tmpTable" style="visibility:hidden">
		<tr>
		<td>
		<input name="assetTransactionItemsForm.companyCode" type="hidden" />
		<input name="assetTransactionItemsForm.companyCodeDisp" type="text" readonly="readonly" disabled="disabled" size="10"/>
	  	</td>
		<td>
		<input name="assetTransactionItemsForm.location.locationId" type="hidden" />
		<input name="assetTransactionItemsForm.assetModel.assetModelId" type="hidden" />
		<input name="assetTransactionItemsForm.location.locationName" type="text" readonly="readonly" disabled="disabled" size="40"/>
		<input name="assetTransactionItemsForm.deleted" type="hidden" />
	  	</td>
	  	<td>
	  	<input name="assetTransactionItemsForm.serialNo" type="hidden"/>
	  	<input name="assetTransactionItemsForm.serialNoDisp" type="text" readonly="readonly" disabled="disabled"/>
	  	</td>
	  	<td>	
	  	<span style="color:red;" class="remarks"></span>
	  	</td>	  
		</tr>
	</table>
	
<script type="text/javascript">

var validateRows = 0;
		$(document).ready(function() {
			
			
		});
		
		function addRow(assetModelId,locationId,locationName,serialNo,companyCode,deleted)
		{
			var row = $('#tmpTable tr').clone();
			var rowSize = $('#initialBalanceTable tbody tr').size();
			
			if(!deleted){
				validateRows++;
				$('.remarks',row).text("");				
			}
			else
			{
				$('.remarks',row).text("Invalid/Duplicate Record. This record wont be inserted.");				
			}
			
			
			$('input[name=assetTransactionItemsForm.assetModel.assetModelId]',row).attr('name','assetTransactionItemsForm['+rowSize+'].assetModel.assetModelId').val(assetModelId);
			$('input[name=assetTransactionItemsForm.location.locationId]',row).attr('name','assetTransactionItemsForm['+rowSize+'].location.locationId').val(locationId);
			$('input[name=assetTransactionItemsForm.location.locationName]',row).attr('name','assetTransactionItemsForm['+rowSize+'].location.locationName').val(locationName);
			$('input[name=assetTransactionItemsForm.serialNo]',row).attr('name','assetTransactionItemsForm['+rowSize+'].serialNo').val(serialNo);
			$('input[name=assetTransactionItemsForm.serialNoDisp]',row).attr('name','assetTransactionItemsForm['+rowSize+'].serialNoDisp').val(serialNo);
			$('input[name=assetTransactionItemsForm.companyCode]',row).attr('name','assetTransactionItemsForm['+rowSize+'].companyCode').val(companyCode);
			$('input[name=assetTransactionItemsForm.companyCodeDisp]',row).attr('name','assetTransactionItemsForm['+rowSize+'].companyCodeDisp').val(companyCode);
			$('input[name=assetTransactionItemsForm.deleted]',row).attr('name','assetTransactionItemsForm['+rowSize+'].deleted').val(deleted);
			
			row.appendTo('#initialBalanceTable tbody');
		}
		
		new AjaxUpload('file-uploader', {
			  action: '<c:url value="/assetModel/importEquipment/${assetModel.assetModelId}" />',
			  // File upload name
			  name: 'excelFile',
			  // Submit file after selection
			  autoSubmit: true,
			  responseType: 'json',
			  onSubmit: function(file, extension) {
				  if (! (/^(xls)$/i.test(extension))){
			            // extension is not allowed
			            alert('Error: only .xls allowed');
			            // cancel upload
			            return false;
			       }
				  document.body.style.cursor = 'wait';
				  $('#file-uploader').attr('disabled', true);
				  this.disable();
				  
			  },
			  onComplete: function(file, responseJSON) {
				  this.enable();
				  $('#initialBalanceTable tbody tr').remove();
				  if(responseJSON.result!='')
			      {
			    		for(i=0;i<responseJSON.result.length;i++)
			    		{
			    			var locationId = responseJSON.result[i].location.locationId;
			    			var assetModelId = responseJSON.result[i].assetModel.assetModelId;
			    			var locationName = responseJSON.result[i].location.locationName;
			    			var serialNo = responseJSON.result[i].serialNo;
			    			var companyCode = responseJSON.result[i].companyCode;
			    			var deleted = responseJSON.result[i].deleted;
			    			addRow(assetModelId,locationId,locationName,serialNo,companyCode,deleted);
			    		}
			      }
				  document.body.style.cursor = 'pointer';
				  $('#file-uploader').removeAttr('disabled');

			  }
			});
		
		
		function formSubmit(){
			if(validateForm()){
				var form = $('#form');				
				form.submit();
			}
		}
		
		function validateForm(){
			var res = true;
			if(validateRows<=0)
			{		 
				alert(" No valid record in imported file to start processing Initial Balance");
				res = false;
				return res;
				
			}	
			return res;
			
		}
		
</script>